//
//  client.c
//  iChatRelayServerClient
//
//  Created by pohanlee on 2014/5/20.
//  Copyright (c) 2014年 iChat. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>

#define SERV_PORT 5566
#define MAXDATA   1024


#define MAXNAME 1024
int main(int argc, char* argv[])
{
    int fd;      /* fd into transport provider */
    int i;      /* loops through user name */
    int length;     /* length of message */
    int fdesc;     /* file description */
    int ndata;     /* the number of file data */
    char data[MAXDATA]; /* read data form file */
    char data1[MAXDATA];  /*server response a string */
    char buf[BUFSIZ];     /* holds message from server */
    struct hostent *hp;   /* holds IP address of server */
    struct sockaddr_in client_addr;   /* address that client uses */
    struct sockaddr_in servaddr; /* the server's full addr */
    
    
    /*
     * Check for proper usage.
     */
    if (argc < 3) {
        fprintf (stderr,
                 "Usage: %s host_name(IP address) file_name\n", argv[0]);
        exit(2);
    }
    /*
     *  Get a socket into TCP/IP
     */
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror ("socket failed!");
        exit(1);
    }
    /*
     * Bind to an arbitrary return address.
     */
    bzero((char *)&client_addr, sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    client_addr.sin_port = htons(0);
    
    
    if (bind(fd, (struct sockaddr *)&client_addr,
             sizeof(client_addr)) <0) {
        perror("bind failed!");
        exit(1);
    }
    /*
     * Fill in the server's address and the data.
     */
    
    
    bzero((char *)&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    
    
    hp = gethostbyname(argv[1]);
    if (hp == 0) {
        fprintf(stderr,
                "could not obtain address of %s\n", argv[2]);
        return (-1);
    }
    
    
    bcopy(hp->h_addr_list[0], (caddr_t)&servaddr.sin_addr,
          hp->h_length);
    /*
     * Connect to the server連線.
     */
    if (connect(fd, (struct sockaddr *)&servaddr,
                sizeof(servaddr)) < 0) {
        perror("connect failed!");
        exit(1);
    }
    /**開起檔案讀取文字 **/
    fdesc = open(argv[2], O_RDONLY);
    if (fdesc == -1) {
        perror("open file error!");
        exit (1);
    }
    ndata = read (fdesc, data, MAXDATA);
    if (ndata < 0) {
        perror("read file error !");
        exit (1);
    }
    data[ndata] = '\0';
    
    
    /* 發送資料給 Server */
    if (write(fd, data, ndata) == -1) {
        perror("write to server error !");
        exit(1);
    }
    /** 由伺服器接收回應 **/
    if (read(fd, data1, MAXDATA) == -1) {
        perror ("read from server error !");
        exit (1);
    }
    /* 印出 server 回應 **/
    printf("%s\n", data1);
    
    
    close (fd);
}



